<?php
/**
 * xStatic PHP 1.0.1a
 * The xStatic Function Library: Shortcuts for common output returns and echoes.
 * Copyright (c)2008 David Christian Liedle
 * 
 * xStatic is a powerful, wrist-friendly, lightweight XHTML authoring tool. It
 * comes standard with the NMOX PHP-MAX Server. xStatic PHP can also be used by
 * itself: just include this file by itself in your PHP page, and you've got it!
 *
 *             Syntactic sugar? Definitely. Sweet? Totally.
 *                        ____  _        _   _      
 *                 __  __/ ___|| |_ __ _| |_(_) ___ 
 *                 \ \/ /\___ \| __/ _` | __| |/ __|
 *                  >  <  ___) | || (_| | |_| | (__ 
 *                 /_/\_\|____/ \__\__,_|\__|_|\___|
 * 
 * 
 * @author David Christian Liedle <david.liedle@gmail.com>
 * @version 1.0.2a, 07/01/08
 * @link http://nmox.org/
 * 
 */

################################################################################
# xStatic can be set to return or echo on function calls. Don't change the
# default below; just override the value of $xstatic_mode within your pages
# by setting $xstatic_mode = "echo"; if you want the alternate behaviour.
#

$xstatic_mode = "return"; // Mode can be set to "echo" or "return"
$xstatic_wrap = "";       // Set to spaces, newlines, etc. as desired.


################################################################################
# The main xStatic function:
#

    function xstatic( $spark ){
        
        // Get the control variables for XStatic:
        global $xstatic_mode; // $xstatic_flip was removed in v1.0.1a
        global $xstatic_wrap;   
        
        $spark = $xstatic_wrap.$spark.$xstatic_wrap;
        
        if( $xstatic_mode=="echo" ){
            echo $spark;
        }
        
        return $spark;
    }


################################################################################
# All xStatic functions, in alphabetical order:
#

function xA(){
    // TODO: Create and wrap <a tags. (For anchors/links).
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xATT($attributes){
    if( is_array($attributes) ){
        foreach( $attributes as $key=>$value ){
            $fret .= ' '.$key.'="'.$value.'"';
        }
    } else {
        $fret = ' '.$attributes;
    }
    return $fret;
}
function xB($bold){
    return xstatic("<b>$bold</b>");
}
function xBAG($add=FALSE){
    global $xstatic_bag;
    if( $add ){
        $xstatic_bag .= $add;
        return $add;
    } else {
        return xstatic($xstatic_bag);
    }
}
function xBIG(){
    // TODO: Create and wrap <big> tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xBODY(){
    // TODO: create and wrap body tags
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xBR(){
    return xstatic("<br />\n");
}
function xBRACK($content){
    return xstatic('[ '.$content.' ]');
}
function xBRAK($content){
    return xBRACK($content);
}
function xCAB($add=FALSE){
    global $xstatic_cab;
    if( $add ){
        $xstatic_cab[] = $add;
        return $add;
    } else {
        return $xstatic_cab;
    }
}
function xCENTER(){
    // TODO: Create and wrap center tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xChar($char){
    switch( $char ){
    // TODO: Figure out how to use an OR evaluation within switch!
        case ("copy" || "(c)"): //<---PROBLEM WITH ORs!!!
            $char = "&copy;";
            break;
        case ("endash" || "-"):
            $char = "&#8211";
            break;
        case ("emdash" || "--"):
            $char = "&#8212;";
            break;
        default:
            $char = "[?]";
    
    }
    return xstatic($char);
}
function xCODE($content){
    return xstatic( "<code>$content</code>" );
}
function xCOMMENT($comment){
    return xstatic( '<!-- '.$comment.' -->' );
}
function xCSSL($link, $media='all'){
    $fret  = '<link type="text/css" href="'.$link.'"';
    $fret .= ' rel="stylesheet" media="'.$media.'" />';
    return xstatic($fret);
}
function xD($downstream=''){
    return xstatic("<a href=\"./$downstream\">./$downstream</a>");
}
function xDD($downstream=''){
    return xstatic("<a href=\"../$downstream\">../$downstream</a>");
}
function xDENT($payload, $tabs=2, $spaces=0){
    $bump = null;
    $fret = null;
    for( $tabs; $tabs>0; $tabs-- ){
        $bump .= '    ';
    }
    for( $spaces; $spaces>0; $spaces-- ){
        $bump .= ' ';
    }
    if( is_array($payload) ){
        foreach( $payload as $val ){
            $fret .= $bump.$val."\n";
        }
    } else {
        $fret = $bump.$payload."\n";
    }
    return xstatic($fret);
}
function xDIV($contents, $id=FALSE, $class=FALSE){
    $d = ( $id ) ? " id=\"$id\"" : null;
    $c = ( $class ) ? " class=\"$class\"" : null;
    return xstatic("<div".$d.$c.">$contents</div>");
}
function xDL(){
    // TODO: Create and wrap DL tags. (Definition list: DL, DT, DD tags).
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xDOC($doctype){
    // TODO: give choice of doctypes
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xDOCT($doctype){
    return xDOC($doctype);
}
function xDx($x, $downstream=''){

    for( $x; $x>0; $x-- ){
        $Dx .= '../';
    }
    
    return xL($Dx . $downstream);

}
function xEM($emphasis){
    return xstatic("<em>$emphasis</em>");
}
function xEMAIL($to_address){
    return xstatic(" <a href=\"mailto:$to_address\">$to_address</a> ");
}
function xEMBED(){
    // TODO: Create and wrap embed tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xER($error){
    return xstatic("\n<p style=\"color: red;\">[xStatic ER] ".$error."</p>\n");
}
function xERR($error){
    return xER($error);
}
function xFORM($attributes){
    // TODO: get attributes working
    return xstatic( "<form>$content</form>" );
}
function xH($headline, $number=1){
    if( $number < 1 ) $number = 1;
    if( $number >6) $number = 6;
    return xstatic("<h".$number.">".$headline."</h".$number.">\n");
}
function xH1($headline){
    return xH($headline, 1);
}
function xH2($headline){
    return xH($headline, 2);
}
function xH3($headline){
    return xH($headline, 3);
}
function xH4($headline){
    return xH($headline, 4);
}
function xH5($headline){
    return xH($headline, 5);
}
function xH6($headline){
    return xH($headline, 6);
}
function xHEAD(){
    // TODO: Create and wrap head tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xHR(){
    return xstatic("<hr />\n");
}
function xHTML(){
    // TODO: create and wrap HTML tags
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xI($italic){
    return xstatic("<i>$italic</i>");
}
function xIN($attributes){
    // TODO: Create and wrap input tags.
    return xINPUT($attributes);
}
function xINPUT($attributes){
    // TODO: Create and wrap input tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xIP(){
    return xstatic($_SERVER['REMOTE_ADDR']);
}
function xJSL($link){
    return xstatic('<script type="text/javascript" src="'.$link.'"></script>');
}
function xL($link){
    return xstatic("<a href=\"$link\">$link</a>");
}
function xLI($content){
    return xstatic('<li>'.$content.'</li>');
}
function xLR($payload){
    return xstatic( ltrim(rtrim($payload)) );
}
function xM($to_address){
    return xEMAIL($to_address);
}
function xMARQ($marquee){
    return xMARQUEE($marquee);
}
function xMARQEE($marquee){
    return xstatic('<marquee>'.$marquee.'</marquee>');
}
function xMENU($content){
    return xstatic('<menu>'.$content.'</menu>');
}
function xMETA($attributes){
    // TODO: Create and wrap meta tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xMETHOD($mode){
    return xMODE($mode);
}
function xMODE($mode){
    global $xstatic_mode;
    switch( $mode ){
        case 'echo':
            xER('Warning - xStatic Echo Mode Untested...');
            $xstatic_mode = 'echo';
            break;
        case 'return':
            $xstatic_mode = 'return';
            break;
        default:
            $xstatic_mode = 'return';
    }
    return $xstatic_mode;
}
function xOL($content){
    return xstatic('<ol>'.$content.'</ol>');
}
function xOPT($attributes){
    // TODO: Create and wrap option tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xOPTION($attributes){
    return xOPT($attributes);
}
function xP($paragraph){
    return xstatic("<p>".$paragraph."</p>\n");
}
function xPRE($content){
    return xstatic( "<pre>$content</pre>" );
}
function xRH(){
    return xstatic( gethostbyaddr(xIP()) );
}
function xS($strong){
    return xSTRONG($strong);
}
function xSMALL($content){
    return xstatic('<small>'.$content.'</small>');
}
function xSPAN($content, $id=FALSE, $class=FALSE){
    $extras = null;
    if( $id ) $extras .= ' id="'.$id.'"';
    if( $class ) $extras .= ' class="'.$class.'"';
    return xstatic('<span'.$extras.'>'.$content.'</span>');
}
function xST($content, $soft_tabs=1){
    for( $soft_tabs; $soft_tabs > 0; $soft_tabs-- ){
        $content = '    '.$content;
    }
    return xstatic($content);
}
function xSTRONG($strong){
    return xstatic("<strong>$strong</strong>");
}
function xT($content, $tabs=1){
    for( $tabs; $tabs > 0; $tabs-- ){
        $content = "\t".$content;
    }
    return xstatic($content);
}
function xTAG($content, $tag='span', $attributes=FALSE){
    // TODO: add $attributes
    return xstatic("<$tag>".$content."</$tag>");
}
function xTABLE($content, $border=TRUE, $attributes=FALSE){
    // TODO: add $attributes
    if( $border ) $border = ' border="1"';
    return xstatic('<table'.$border.'>'.$content.'</table>');
}
function xTD($content, $attributes=FALSE){
    // TODO: add $attributes
    return xstatic('<td>'.$content.'</td>');
}
function xTH($content, $attributes=FALSE){
    // TODO: add $attributes
    return xstatic('<th>'.$content.'</th>');
}
function xTITLE(){
    // TODO: Create and wrap title tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xTR($content, $attributes=FALSE){
    // TODO: add $attributes
    return xstatic('<tr>'.$content.'</tr>');
}
function xTT(){
    // TODO: Create and wrap teletype tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xU(){
    // TODO: Create and wrap underline tags.
    return xER('Not yet implemented. Check out line '.__LINE__.' to view.');
}
function xUA(){
    return xstatic( $_SERVER['HTTP_USER_AGENT'] );
}
function xUL($content){
    return xstatic('<ul>'.$content.'</ul>');
}
function xUNA($array=FALSE){
    global $xstatic_cab;
    if( !$array ){
        if( is_array($xstatic_cab) ){
            $array = $xstatic_cab;
        } else {
            return xstatic( xER("xUNA Error: \$xstatic_cab array not set.") );
        }
    }
    if( is_array( $array ) ){
        foreach( $array as $ar ){
            $fret .= $ar."\n";
        }
    } else {
        $fret = xER("xUNA Error: The variable received is not an array.");
    }
    return xstatic($fret);
}
function xWIK($enArticleName, $linkText=FALSE){
    return xL("http://en.wikipedia.org/wiki/".$enArticleName);
}
function xWRAP($content, $wrapper=' '){
    return xstatic($wrapper.$content.$wrapper);
}
function xYear(){
    return xstatic(date('Y'));
}
################################################################################
# Just for kicks, to watch the chain:
#
locateMe("/nmox/main/xstatic.php");
